package bar.dark.lee.memo;

import java.lang.reflect.Field;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.CalendarPickerDialog;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class MemoSelectActivity extends SuperActivity
{
	private DBSQLite sql;
	
	private View startDateView;
	private TextView startDateTextView;
	
	private View endDateView;
	private TextView endDateTextView;
	
	private EditText titleEditText;
	private EditText memoEditText;
	
	private Button selectButton;
	
	private int startYear;
	private int startMonth;
	private int startDay;
	
	private int endYear;
	private int endMonth;
	private int endDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.memo_select_activity);
		
		setTitle("메모검색");
		
		setFindView();
		setOnClickListener();
		setData();
	}

	private void setFindView()
	{
		startDateView = findViewById(R.id.memoSelectStartDateView);
		startDateTextView = (TextView) findViewById(R.id.memoSelectStartDateTextView);
		
		endDateView = findViewById(R.id.memoSelectEndDateView);
		endDateTextView = (TextView) findViewById(R.id.memoSelectEndDateTextView);
		
		titleEditText = (EditText) findViewById(R.id.memoSelectTitleEditText);
		memoEditText = (EditText) findViewById(R.id.memoSelectMemoEditText);
		
		selectButton = (Button) findViewById(R.id.memoSelectButton);
	}
	
	private void setOnClickListener()
	{
		startDateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
//				DatePickerDialog dialog = new DatePickerDialog(superContext, new OnDateSetListener()
//				{
//					@Override
//					public void onDateSet(DatePicker view, int year, int monthOfYear,
//							int dayOfMonth)
//					{
//						String startDate = BDUtil.getDateDB(year, monthOfYear+1, dayOfMonth);
//						String endDate = BDUtil.getDateDB(endYear, endMonth+1, endDay);
//						
//						if (Integer.parseInt(startDate) > Integer.parseInt(endDate))
//						{
//							BDUtil.showToast(superContext, "시작날짜 보다 종료날짜가 작을 수는 없습니다.");
//							return;
//						}
//						
//						startYear = year;
//						startMonth = monthOfYear;
//						startDay = dayOfMonth;
//						
//						startDateTextView.setText(year + "년 " + (monthOfYear+1) + "월");
//					}
//				}, startYear, startMonth, startDay);
//				
//				try
//				{
//					// 다이얼로그에서 날짜 빼고 나오는 로직
//					Field[] f = dialog.getClass().getDeclaredFields();  
//					
//					for (Field dateField : f) 
//					{   
//						if(dateField.getName().equals("mDatePicker")) 
//						{    
//							dateField.setAccessible(true);             
//							DatePicker datePicker = (DatePicker)dateField.get(dialog); 
//							Field datePickerFields[] = dateField.getType().getDeclaredFields();
//							
//							for(Field datePickerField : datePickerFields) 
//							{     
//								if("mDayPicker".equals(datePickerField.getName())) 
//								{      
//									datePickerField.setAccessible(true);     
//									Object dayPicker = new Object();      
//									dayPicker = datePickerField.get(datePicker);      
//									((View)dayPicker).setVisibility(View.GONE);     
//								}    
//							}   
//						}  
//					}
//				} catch (SecurityException e)
//				{
//					e.printStackTrace();
//				} catch (IllegalArgumentException e)
//				{
//					e.printStackTrace();
//				} catch (IllegalAccessException e)
//				{
//					e.printStackTrace();
//				}
//				
//				dialog.show();
//				
				CalendarPickerDialog cpd = new CalendarPickerDialog(superContext, new CalendarPickerDialog.ICalendarPickerDateSetListener() {
					@Override
					public void onDateSet(int year, int monthOfYear, int dayOfMonth) {
						
						
						String startDate = BDUtil.getDateDB(year, monthOfYear+1, dayOfMonth);
						String endDate = BDUtil.getDateDB(endYear, endMonth+1, endDay);
						
						if (Integer.parseInt(startDate) > Integer.parseInt(endDate))
						{
							BDUtil.showToast(superContext, "시작날짜 보다 종료날짜가 작을 수는 없습니다.");
							return;
						}
						
						startYear = year;
						startMonth = monthOfYear;
						startDay = dayOfMonth;
						
						startDateTextView.setText(year + "년 " + (monthOfYear+1) + "월");
					}
				}, startYear, startMonth, startDay);
				
				cpd.show();
				
			}
		});
		
		endDateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
				CalendarPickerDialog cpd = new CalendarPickerDialog(superContext, new CalendarPickerDialog.ICalendarPickerDateSetListener() {
					@Override
					public void onDateSet(int year, int monthOfYear, int dayOfMonth) {
						
						String endDate = BDUtil.getDateDB(year, monthOfYear+1, dayOfMonth);
						String startDate = BDUtil.getDateDB(startYear, startMonth+1, startDay);
						
						if (Integer.parseInt(startDate) > Integer.parseInt(endDate))
						{
							BDUtil.showToast(superContext, "시작날짜 보다 종료날짜가 작을 수는 없습니다.");
							return;
						}
						
						endYear = year;
						endMonth = monthOfYear;
						endDay = dayOfMonth;
						
						endDateTextView.setText(year + "년 " + (monthOfYear+1) + "월");
					}
				}, endYear, endMonth, endDay);
				
				cpd.show();
				
				
//				DatePickerDialog dialog = new DatePickerDialog(superContext, new OnDateSetListener()
//				{
//					@Override
//					public void onDateSet(DatePicker view, int year, int monthOfYear,
//							int dayOfMonth)
//					{
//						String endDate = BDUtil.getDateDB(year, monthOfYear+1, dayOfMonth);
//						String startDate = BDUtil.getDateDB(startYear, startMonth+1, startDay);
//						
//						if (Integer.parseInt(startDate) > Integer.parseInt(endDate))
//						{
//							BDUtil.showToast(superContext, "시작날짜 보다 종료날짜가 작을 수는 없습니다.");
//							return;
//						}
//						
//						endYear = year;
//						endMonth = monthOfYear;
//						endDay = dayOfMonth;
//						
//						endDateTextView.setText(year + "년 " + (monthOfYear+1) + "월");
//					}
//				}, endYear, endMonth, endDay);
//				
//				try
//				{
//					// 다이얼로그에서 날짜 빼고 나오는 로직
//					Field[] f = dialog.getClass().getDeclaredFields();  
//					
//					for (Field dateField : f) 
//					{   
//						if(dateField.getName().equals("mDatePicker")) 
//						{    
//							dateField.setAccessible(true);             
//							DatePicker datePicker = (DatePicker)dateField.get(dialog); 
//							Field datePickerFields[] = dateField.getType().getDeclaredFields();
//							
//							for(Field datePickerField : datePickerFields) 
//							{     
//								if("mDayPicker".equals(datePickerField.getName())) 
//								{      
//									datePickerField.setAccessible(true);     
//									Object dayPicker = new Object();      
//									dayPicker = datePickerField.get(datePicker);      
//									((View)dayPicker).setVisibility(View.GONE);     
//								}    
//							}   
//						}  
//					}
//				} catch (SecurityException e)
//				{
//					e.printStackTrace();
//				} catch (IllegalArgumentException e)
//				{
//					e.printStackTrace();
//				} catch (IllegalAccessException e)
//				{
//					e.printStackTrace();
//				}
//				
//				dialog.show();
			}
		});
		
		selectButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(MemoListActivity.START_YEAR, startYear);
				intent.putExtra(MemoListActivity.START_MONTH, startMonth);
				intent.putExtra(MemoListActivity.END_YEAR, endYear);
				intent.putExtra(MemoListActivity.END_MONTH, endMonth);
				intent.putExtra(MemoListActivity.TITLE_SELECT, titleEditText.getText().toString());
				intent.putExtra(MemoListActivity.MEMO_SELECT, memoEditText.getText().toString());
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	private void setData()
	{
		Calendar cal = Calendar.getInstance();
		
		startYear = cal.get(Calendar.YEAR);
		startMonth = cal.get(Calendar.MONTH);
		startDay = cal.get(Calendar.DAY_OF_MONTH);
		
		endYear = cal.get(Calendar.YEAR);
		endMonth = cal.get(Calendar.MONTH);
		endDay = cal.get(Calendar.DAY_OF_MONTH);
		
		startDateTextView.setText(startYear + "년 " + (startMonth+1) + "월");
		endDateTextView.setText(endYear + "년 " + (endMonth+1) + "월");
	}
	
	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_MEMO);
		startActivity(intent);
	}
}
